package com.lvchen.ui.main;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.widget.Toast;

import com.lvchen.R;
import com.lvchen.common.base.AppManager;
import com.lvchen.common.base.BaseActivity;
import com.lvchen.ui.fourfunction.FourFragment;
import com.lvchen.ui.login.MyWelcomActivity;
import com.lvchen.ui.onefunction.OneFragment;
import com.lvchen.ui.threefunction.ThreeFragment;
import com.lvchen.ui.twofunction.TwoFragment;
import com.next.easynavigition.constant.Anim;
import com.next.easynavigition.view.NavigitionBar;
import com.stephentuso.welcome.WelcomeHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 主类
 */
public class MainActivity extends BaseActivity{

    private NavigitionBar nb_main;

    private String[] tabText = {"首页", "发现", "消息", "我的"};
    //未选中icon
    private int[] normalIcon = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    //选中时icon
    private int[] selectIcon = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};

    private List<Fragment> fragments = new ArrayList<Fragment>();

    private long exitTime = 0;

    private WelcomeHelper helper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ensureUi();



        helper = new WelcomeHelper(this, MyWelcomActivity.class);
        helper.show(savedInstanceState);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        helper.onSaveInstanceState(outState);
    }

    /**
     * 初始化页面
     * */
    private void ensureUi(){

        nb_main = (NavigitionBar) findViewById(R.id.nb_main);

        fragments.add(new OneFragment());
        fragments.add(new TwoFragment());
        fragments.add(new ThreeFragment());
        fragments.add(new FourFragment());

        nb_main.setData(tabText, normalIcon, selectIcon, fragments, getSupportFragmentManager(), Anim.Bounce,false);
        nb_main.setMsgPointCount(2, 109);
        nb_main.setHintPoint(3, true);
        nb_main.getmViewPager().setCanScroll(false);//是否可以左右滑动

    }

    public static void goToThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context,MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(MainActivity.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                AppManager.getAppManager().finishActivity();;
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
